package com.sharker.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sharker.R;
import com.sharker.activity.BannerWebviewActivity;
import com.sharker.activity.CourseDetailActivity;
import com.sharker.activity.TopicallActivity;
import com.sharker.adapter.KechengAdapter;
import com.sharker.adapter.MyAdapter;
import com.sharker.adapter.TopicAdapter;
import com.sharker.models.data.BaseData;
import com.sharker.models.data.CourseData;
import com.sharker.models.data.TopicData;
import com.sharker.models.data.TryData;
import com.sharker.network.SharkerParams;
import com.sharker.view.CustomProgressDialog;
import com.sharker.wiget.DividerItemDecoration;
import com.sharker.wiget.FullyLinearLayoutManager;
import com.sharker.wiget.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 项目名称：Sharker-master
 * 类描述：
 * 创建人：${丁江楠}
 * 创建时间：2017/4/7 19:30
 * 修改人：${丁江楠}
 * 修改时间：2017/4/7 19:30
 * 修改备注：
 */
public class LearnFragment extends Fragment implements View.OnClickListener{

    @BindView(R.id.topic_all)
    TextView topic_all;
    @BindView(R.id.mBanner)
    Banner mBanner;
    @BindView(R.id.imageview_sanjiao)
    ImageView imageviewSanjiao;
    @BindView(R.id.imageview_jian)
    ImageView imageviewJian;
    @BindView(R.id.try_all)
    TextView try_all;
    @BindView(R.id.try_recyclerview)
    RecyclerView tryRecyclerview;
    @BindView(R.id.imageview_sanjiao2)
    ImageView imageviewSanjiao2;
    @BindView(R.id.imageview_jian2)
    ImageView imageviewJian2;
    @BindView(R.id.course_recyclerview)
    RecyclerView courseRecyclerview;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.iv2)
    ImageView iv2;
    @BindView(R.id.topic_recyclerview)
    RecyclerView topicRecyclerview;
    private LinearLayoutManager linearLayoutManager;
    private MyAdapter myAdapter;
    private List<String> list=new ArrayList<>();
    private CustomProgressDialog dialog;
    private FullyLinearLayoutManager fullyLinearLayoutManager;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_learn, null);
        unbinder = ButterKnife.bind(this, view);
        //加载动画
        dialog = new CustomProgressDialog(getActivity(), R.drawable.frame);
        dialog.show();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

            initview();
            listBanner();
            listTry();
            listCourse("list_course","",0,1);
            listTopic("list_topic","",0,0);

    }
        private void initview () {
            try_all.setOnClickListener(this);
            topic_all.setOnClickListener(this);
            mBanner.setImageLoader(new GlideImageLoader());
        }
    void listBanner() {
        SharkerParams params = new SharkerParams("list_banner");
        x.http().post(params, new Callback.CommonCallback<BaseData>() {

            private String image;

            @Override
            public void onSuccess(BaseData result) {
                final List<com.sharker.models.Banner> banner = result.banner;
                for(int i=0;i<banner.size();i++){
                    image = banner.get(i).image;
                    image.replace("\\","");
                    list.add(image);
                }
                mBanner.setImages(list).setIndicatorGravity(BannerConfig.LEFT);
                mBanner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        Intent intent=new Intent(getActivity(), BannerWebviewActivity.class);
                        intent.putExtra("click",banner.get(position).click);
                        startActivity(intent);
                    }
                });
                mBanner.start();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    void listTry() {
        SharkerParams params = new SharkerParams("list_try");
//      params.addBodyParameter("category","001");
        params.addBodyParameter("page_size", "20");
        params.addBodyParameter("page_index", "0");
        x.http().post(params, new Callback.CommonCallback<TryData>() {
            @Override
            public void onSuccess(final TryData result) {
                linearLayoutManager = new LinearLayoutManager(getActivity()) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                };
                tryRecyclerview.setLayoutManager(linearLayoutManager);
                tryRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
                myAdapter = new MyAdapter(result.getTryList(), getActivity());
                tryRecyclerview.setAdapter(myAdapter);
                //试听条目点击
                myAdapter.setMyOnitemListener(new MyAdapter.MyOnitemListener() {
                    @Override
                    public void OnItemclick(View view, int position) {
                  /*     Intent intent = new Intent(getActivity(), .class);
                   //  intent.putExtra("object_id", result.getTryList().get(position).getObject_id());
                        // startActivity(intent);*/
                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //精选课程列表
    public void listCourse(String uri, String category, int page_index, int sort_by) {

        SharkerParams params = new SharkerParams(uri);

        if (!TextUtils.isEmpty(category)) {
            params.addBodyParameter("category", category);
        }
        params.addBodyParameter("page_size", "25");

        params.addBodyParameter("page_index", "" + page_index);

        if (sort_by != 0) {
            params.addBodyParameter("sort_by", "" + sort_by);
        }

        x.http().post(params, new Callback.CommonCallback<CourseData>() {
             @Override
            public void onSuccess(CourseData result) {
                //dialog.dismiss();
                LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity()) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                };
                courseRecyclerview.setLayoutManager(linearLayoutManager1);
                courseRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
                courseRecyclerview.setAdapter(new KechengAdapter(getActivity(), result.getCourse()));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //精选专辑列表
    public void listTopic(String uri, String category, int page_index, int sort_by) {

        SharkerParams params = new SharkerParams(uri);

        if (!TextUtils.isEmpty(category)) {
            params.addBodyParameter("category", category);
        }

        params.addBodyParameter("page_size", "25");

        params.addBodyParameter("page_index", "" + page_index);

        if (sort_by != 0) {
            params.addBodyParameter("sort_by", "" + sort_by);
        }

        x.http().post(params, new Callback.CommonCallback<TopicData>() {
            @Override
            public void onSuccess(TopicData result) {
                dialog.dismiss();
                topicRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 3));
                TopicAdapter topicAdapter = new TopicAdapter(getActivity(), result.getTopic());
                topicRecyclerview.setAdapter(topicAdapter);
               /* topicAdapter.setTopicOnitemListener(new TopicAdapter.TopicOnitemListener() {
                    @Override
                    public void OnItemclick(View view, int position) {
                        Intent intent=new On
                    }
                });*/

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                new CountDownTimer(3000, 1000) {
                    @Override
                    public void onTick(long l) {

                    }

                    @Override
                    public void onFinish() {
                        Toast.makeText(getActivity(),"加载失败",Toast.LENGTH_LONG).show();

                    }
                };

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.try_all:
                Intent intent = new Intent(getActivity(), CourseDetailActivity.class);
                // intent.putExtra("object_id", result.getTryList().get(position).getObject_id());
                startActivity(intent);


                break;
            case R.id.topic_all:
                Intent intent1=new Intent(getActivity(), TopicallActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
